#include <iostream>
#include <cstring>
using namespace std;

const int N = 510;
int dp[N][N];
int a[N][N];
int n , m;

int main()
{
    cin >> n >> m;
    memset(dp , 0x3f3f3f3f , sizeof dp);
    for(int i = 1;i <= n;i ++)
        for(int j = 1;j <= m;j ++)
            cin >> a[i][j];
    dp[0][1] = 0;
    for(int i = 1;i <= n;i ++)
    {
        for(int j = 1;j <= m;j ++)
        {
            dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + a[i][j];
        }
    }
    cout << dp[n][m] << endl;
    return 0;
}